const crossword = {
    dataword: null,
    chooseArry: [
        '',
        '',
        '',
        '',
        '',
        '',
        ''
    ],
    openid:"1111",
    post_id: '',
    word_container: "#word_container",
    word_app: "#word_app",
    gameReset: '#gameReset',
    pushInfo: '#pushInfo',
    pop_title: '.pop-title',
    pop_up_window: ".pop-up-window",
    ruler_btn: '.ruler-btn',
    correctAn: '#correctAn',
    prizeName: '#prizeName',
    l_close: 'l_close', //猜中
    u_close: 'u_close', //猜中,没中奖
    b_close: 'b_close', //没猜中
    game_ruler: '.game-ruler',
    g_close: '.g_close',
    ruler_btn: '.ruler-btn',
    wordInitUrl: "../json/data.json",
    // wordInitUrl:"http://10.122.202.211:8080/woAH-wap/poem/getRandomPoem.action",
    //

    priceUrl: "../json/price.json",
    // priceUrl: "http://10.122.202.211:8080/woAH-wap/poem/getPrizeInfo.action",
    windowPanel: {
        l_close: ".l-window",
        u_close: ".u-window",
        b_close: ".b-window"
    },
    init: function() {
        this.getWord();
        this.closePop(this.l_close);
        this.closePop(this.u_close);
        this.closePop(this.b_close);
        this.closeRuler(this.g_close);
        this.rulerClick();
        this.resetPage();
        this.pushWord();
    },
    //绑定弹窗关闭按钮
    closeRuler: function(btn) {
        var context = this;
        var close_parent = $(btn).attr('data-parent')
        $(close_parent).click(function(e) {
            console.log(close_parent);
            e.preventDefault();
            $(close_parent).fadeOut(300)
        })
    },
    //重置页面
    resetPage: function() {
        var context = this;
        $(context.gameReset).click(function(e) {
            e.preventDefault();
            console.log('11');
            context.chooseArry = [
                '',
                '',
                '',
                '',
                '',
                '',
                ''
            ];
            context.add2check();

        })
    },
    //提交信息
    pushWord: function() {
        var context = this;
        $(context.pushInfo).click(function(e) {
            e.preventDefault();

            var postStr = context.chooseArry.join('').trim();
            console.log(postStr);
            if (7 != postStr.length) {
                layer_msg('请填写完毕后提交');
            } else {
                $.ajax({
                    type: "GET",
                    url: context.priceUrl,
                    contentType: "application/json;charset=UTF-8",
                    data: JSON.stringify({"id": context.post_id , "openid": context.openid, "phone": sessionStorage.getItem('phone'),"poem": postStr}),
                    success: function(response) {
                        // 根据返回值判弹窗
                        response = response.data;
                        // 对应的弹窗
                        // 1:正确
                        // 2：正确但是未中奖
                        // 3.错误
                        var openArry = [context.windowPanel.u_close, context.windowPanel.l_close, context.windowPanel.b_close];
                        $(openArry[1]).parent().parent().fadeIn();
                        $(openArry[response.type]).fadeIn();
                        // context.wordChoose();
                        if (2 == response.type) {
                            $(context.correctAn).html(response.rightAnswer)
                        } else if (1 == response.type) {
                            $(context.prizeName).html(response.prizeName)
                        } else if (0 == response.type) {} else {
                            layer_msg(response);
                        }
                    },
                    error: function() {}
                })
            }
        })

    },
    //点击活动规则
    rulerClick: function() {
        var context = this;
        $(context.ruler_btn).click(function(e) {
            console.log('1');
            e.preventDefault();
            $(context.game_ruler).fadeIn(300);
        });
    },
    showPop: function(data) {
        var context = this;
        $(context.pop_up_window).fadeIn(300);
        $(context.pop_info).html(data.msg);
    },
    //绑定弹窗关闭按钮
    closePop: function(btn) {
        var context = this;
        console.log(btn);
        var close_parent = context.windowPanel[btn];
        $("." + btn).click(function(e) {
            console.log(close_parent);
            e.preventDefault();
            $(close_parent).parent().parent().fadeOut(300);
        })
    },
    ChooseOrRemove: function() {
        var context = this;
        context.add2check();
        function container2app(e) {
            e.preventDefault();
            var self = this;
            let postStr = context.chooseArry.join('').trim();
            if (7 != postStr.length) {
                $(self).addClass('rubberBand')
                var timer = setTimeout(function() {
                    $(self).removeClass('rubberBand')
                    clearTimeout(timer);
                    timer = null;
                }, 500)
                for (let i = 0; i < 7; i++) {
                    if (!context.chooseArry[i]) {
                        context.chooseArry[i] = context.dataWord[$(self).attr('data-word')];
                        //调用百度语音
                        baiduAudio($(self).text());
                        context.add2check();
                        console.log(context.chooseArry);
                        return;
                    }
                }
            } else {
                layer_msg('已经达到诗词的最大长度')
                return;
            }

        }
        function app2container(e) {
            var self = this;
            e.preventDefault();
            if (!context.chooseArry.length) {
                layer_msg('请添加文字')
                return;
            }
            context.chooseArry.splice($(this).attr('data-word'), 1, '')
            context.add2check();
        }
        $(context.word_container).on('click', 'li>a', container2app);
        $(context.word_app).on('click', 'li>a', app2container)
    },
    getWord: function() {
        var context = this;
        //  $('.intro').append('<div>孤帆远影碧空尽，接下一句</div>')
        // context.dataWord="草师丛接惊鱼现哈蛮易信有".slice('');
        // context.add2container();
        // return;
        $.ajax({
            type: "POST",
            url: context.wordInitUrl,
            contentType: "application/json;charset=UTF-8",
            data: JSON.stringify({"phone": ''}),
            success: function(response) {
                response = response.data;
                context.post_id = response.id;
                context.dataWord = response.randomPoem.slice('');
                //添加到页面
                context.add2container();
                // context.wordChoose();
            },
            error: function() {}
        })
    },
    add2container: function() {
        var context = this;
        var html = '';
        $(context.word_container).html('');
        console.log(context.dataWord);
        for (let i = 0; i < 12; i++) {
            html += `<li><a href="#" class="HYJinChangTiJ word animated" data-word="${i}">${context.dataWord[i]}</a></li>`;
        }
        $(context.word_container).html(html);
        //调用点击事件
        context.ChooseOrRemove();
    },
    add2check: function() {
        var context = this;
        var html = '';
        $(context.word_app).html('');
        console.log(context.dataWord);
        for (let i = 0; i < context.chooseArry.length; i++) {
            html += `<li><a href="#" class="HYJinChangTiJ  choose-word animated"  data-word="${i}">${context.chooseArry[i]}</a></li>`;
        }
        $(context.word_app).html(html);
    },
    removeChcek: function() {
        var context = this;
    }
}
crossword.init();
